function createWallMaterail() {
    const matArrayWall = [];
    matArrayWall.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //ǰ  0xafc0ca :��ɫ
    matArrayWall.push(new THREE.MeshPhongMaterial({color: 0x9cb2d1}));  //��  0x9cb2d1������
    matArrayWall.push(new THREE.MeshPhongMaterial({color: 0xd6e4ec}));  //��  0xd6e4ec�� ƫ��ɫ
    matArrayWall.push(new THREE.MeshPhongMaterial({color: 0xd6e4ec}));  //��
    matArrayWall.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //��   0xafc0ca :��ɫ
    matArrayWall.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //��
    return matArrayWall;
}

function createCube(width, height, depth, angle, x, y, z, name) {
    var cubeGeometry = new THREE.BoxGeometry(width, height, depth);
    var cube = new THREE.Mesh(cubeGeometry, createWallMaterail());
    cube.position.x = x;
    cube.position.y = y;
    cube.position.z = z;
    cube.rotation.y += angle * Math.PI; //
    cube.name = name;
    return cube;
}

function createCubeBsp(wall, cubeList) {
    const material = new THREE.MeshPhongMaterial({
        color: 0x9cb2d1,
        specular: 0x9cb2d1,
        shininess: 30,
        transparent: true,
        opacity: 1
    });
    let BSP = new ThreeBSP(wall);
    for (let i = 0; i < cubeList.length; i++) {
        const less_bsp = new ThreeBSP(cubeList[i]);
        BSP = BSP.subtract(less_bsp);
    }
    const result = BSP.toMesh(material);
    result.material.flatshading = THREE.FlatShading;
    result.geometry.computeFaceNormals(); //���¼��㼸������淨����
    result.geometry.computeVertexNormals();
    result.material.needsUpdate = true; //��������
    result.geometry.buffersNeedUpdate = true;
    result.geometry.uvsNeedUpdate = true;
    return result;
}

function createLeftDoor(scene, width, height, depth, angle, x, y, z, name) {
    var loader = new THREE.TextureLoader();
    loader.load("./ThreeJs/images/door_left.png", function (texture) {
        var doorgeometry = new THREE.BoxGeometry(width, height, depth);
        doorgeometry.translate(50, 0, 0);
        var doormaterial = new THREE.MeshBasicMaterial({
            map: texture,
            color: 0xffffff
        });
        doormaterial.opacity = 1.0;
        doormaterial.transparent = true;
        var door = new THREE.Mesh(doorgeometry, doormaterial);
        door.position.set(x, y, z);
        door.rotation.y += angle * Math.PI; //
        door.name = name;
        scene.add(door);
    });
}

function createRightDoor(scene, width, height, depth, angle, x, y, z, name) {
    var loader = new THREE.TextureLoader();
    loader.load("./ThreeJs/images/door_right.png", function (texture) {
        var doorgeometry = new THREE.BoxGeometry(width, height, depth);
        doorgeometry.translate(-50, 0, 0);
        var doormaterial = new THREE.MeshBasicMaterial({
            map: texture,
            color: 0xffffff
        });
        doormaterial.opacity = 1.0;
        doormaterial.transparent = true;
        var door = new THREE.Mesh(doorgeometry, doormaterial);
        door.position.set(x, y, z);
        door.rotation.y += angle * Math.PI; //
        door.name = name;
        scene.add(door);
    });
}

function createWindow(scene, width, height, depth, angle, x, y, z, name) {
    var loader = new THREE.TextureLoader();
    loader.load("./ThreeJs/images/window.png", function (texture) {
        var windowgeometry = new THREE.BoxGeometry(width, height, depth);
        var windowmaterial = new THREE.MeshBasicMaterial({
            map: texture,
            color: 0xffffff
        });
        windowmaterial.opacity = 1.0;
        windowmaterial.transparent = true;
        var window = new THREE.Mesh(windowgeometry, windowmaterial);
        window.position.set(x, y, z);
        window.rotation.y += angle * Math.PI; //
        window.name = name;
        scene.add(window);
    });
}

function createFloor(scene, length, width, height) {
    var loader = new THREE.TextureLoader();
    loader.load("./ThreeJs/images/floor.jpg", function (texture) {
        texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
        texture.repeat.set(10, 10);
        var floorGeometry = new THREE.BoxGeometry(length,
            width, height);
        var floorMaterial = new THREE.MeshBasicMaterial({
            map: texture,
        });
        var floor = new THREE.Mesh(floorGeometry, floorMaterial);
        floor.rotation.x = -Math.PI / 2;
        floor.name = "地板";
        scene.add(floor);
    });
}


function createFrame(scene, qu, gdl, reversal, width, height, deep, x, y, z, angle, count, name) {
    var outer = new THREE.Group();
    outer.gdl = gdl;
    outer.qu = qu;
    let frameTotalWidth = width * count;
    for (var i = 0; i < count; i++) {
        var group = new THREE.Group();
        group.groupId = i;
        group.name = "group-" + i;
        let textureLoader = new THREE.TextureLoader();
        let materialRight = null;
        if (i === count-1) {
            materialRight = new THREE.MeshBasicMaterial({
                map: textureLoader.load('./ThreeJs/images/frame_left.jpg'),
                side: THREE.DoubleSide
            });
        }
        let materialLeft = null;
        if (i === 0) {
            materialLeft = new THREE.MeshBasicMaterial({
                map: textureLoader.load('./ThreeJs/images/frame_left.jpg'),
                side: THREE.DoubleSide
            });
        }
        let mesh = new THREE.Mesh(
            new THREE.BoxGeometry(width, height, deep),
            [
                materialRight,
                materialLeft,
                new THREE.MeshBasicMaterial({
                    map: textureLoader.load('./ThreeJs/images/frame_top.jpg'),
                    side: THREE.DoubleSide
                }),
                new THREE.MeshBasicMaterial({
                    map: textureLoader.load('./ThreeJs/images/box.png'),
                    side: THREE.DoubleSide
                }),
                new THREE.MeshBasicMaterial({
                    map: textureLoader.load(i === gdl ? './ThreeJs/images/frame_front_fix.jpg' : './ThreeJs/images/frame_front.jpg'),
                    side: THREE.DoubleSide
                }),
                new THREE.MeshBasicMaterial({
                    map: textureLoader.load('./ThreeJs/images/frame_back.jpg'),
                    side: THREE.DoubleSide
                }),
            ]
        );
        mesh.position.set(reversal ? (frameTotalWidth + x - width * (i + 1)) : (x + width * i), y, z);
        mesh.name = name;
        group.add(mesh);
        for (var j = 0; j < 3; j++) {
            var cube = new THREE.BoxGeometry(width - 10, 5, deep - 4);
            var cubeMaterial = new THREE.MeshPhongMaterial({
                color: 0xaaaaaa,
                specular: 0x009900,
                shininess: 30,
                flatShading: true
            });
            let mesh2 = new THREE.Mesh(cube, cubeMaterial);
            mesh2.position.set(reversal ? (frameTotalWidth + x - width * (i + 1)) : (x + width * i),
                height / 4 + j * height / 4, z);
            group.add(mesh2);
            // for (var k = 0; k < 14; k++) {
            //     var cubeBook = new THREE.BoxGeometry(25, 40, 5);
            //     var cubeMaterialBook = new THREE.MeshBasicMaterial({
            //         color: 0xff00ff
            //     });
            //     let mesh3 = new THREE.Mesh(cubeBook, cubeMaterialBook);
            //     mesh3.position.set(x + width * i, height / 4 + j * height / 4 + 20, z - (deep - 4) / 2 + 10 * (k + 1));
            //     group.add(mesh3);
            // }
        }

        // new THREE.FontLoader().load('./ThreeJs/FZYaoTi_Regular.json', function (font) {
        //     var text = new THREE.TextGeometry(qu + "区", {
        //         font: font,
        //         size: 60,
        //         height: 10
        //     });
        //     text.computeBoundingBox();
        //     var textMaterial = new THREE.MeshStandardMaterial({color: "#a0a0a0"});
        //     var textMesh = new THREE.Mesh(text, textMaterial);
        //
        //     textMesh.position.set(x + width * count / 2, y + height, z - 30);
        //     textMesh.rotation.y += Math.PI * angle;
        //     textMesh.rotation.x += Math.PI * (-0.5 + angle);
        //     scene.add(textMesh);
        // });
        mesh.rotation.y += Math.PI * angle;

        outer.add(group);
    }
    scene.add(outer);
    return outer;
}


function createHorizontalTrack(scene, x, y, z, length) {
    var cube = new THREE.BoxGeometry(length, 5, 20);
    var cubeMaterial = new THREE.MeshPhongMaterial({
        color: 0x3d3d3d,
        specular: 0x009900, shininess: 30, flatShading: true
    });

    let track1 = new THREE.Mesh(cube, cubeMaterial);
    let track2 = new THREE.Mesh(cube, cubeMaterial);
    let track3 = new THREE.Mesh(cube, cubeMaterial);


    track1.position.set(x, y, z);
    // track2.position.set(x, y, z+40);
    track3.position.set(x, y, z + 80);
    cube.length = length;
    var group = new THREE.Group();
    group.add(track1);
    // group.add(track2);
    group.add(track3);
    scene.add(group);
    return track1;
}






